package barbershop.com.service.orders.impl;

import barbershop.com.dao.orders.OrderDao;
import barbershop.com.dao.orders.OrderItemDao;
import barbershop.com.entity.orders.OrderItem;
import barbershop.com.service.orders.OrderItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Transactional
@Service
public class OrderItemServiceImpl implements OrderItemService {
    @Autowired
    private OrderItemDao orderItemDao;

    @Override
    public OrderItem insert(OrderItem record) {
        try {
            OrderItem orderItem = this.orderItemDao.save(record);
            System.out.println("订单条目添加成功！");
            return orderItem;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单条目添加失败！");
        }
        return null;
    }

    @Override
    public Boolean delete(OrderItem record) {
        try {
            this.orderItemDao.delete(record);
            System.out.println("订单条目删除成功！");
            return true;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单条目删除失败！");
            return false;
        }
    }

    @Override
    public OrderItem update(OrderItem record) {
        try {
            OrderItem orderItem = this.orderItemDao.save(record);
            System.out.println("订单条目修改成功！");
            return orderItem;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单条目修改失败！");
        }
        return null;
    }

    @Override
    public OrderItem find(OrderItem record) {
        try {
            Optional<OrderItem> orderItem = this.orderItemDao.findById(record.getOrderItemId());
            if (orderItem.isPresent()){
                System.out.println("订单条目查询成功！");
                return orderItem.get();
            }
            return null;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单条目查询失败！");
        }
        return null;
    }

    @Override
    public void updateQuantity(Integer quantity, Integer id) {
        try {
            this.orderItemDao.updateQuantity(quantity, id);
            System.out.println("数量修改成功！");
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("数量修改失败！");
        }
    }

    @Override
    public List<OrderItem> updateOrderItem(List<OrderItem> orderItemList) {
        try {
            List<OrderItem> newOrderItemList = this.orderItemDao.saveAll(orderItemList);
            System.out.println("批量插入订单条目成功！");
            return newOrderItemList;
        }   catch (Exception e){
            System.out.println("批量插入订单条目失败！");
        }
        return null;
    }

    @Override
    public void calAllSubTotalMoney(List<OrderItem> orderItemList) {
        try {
            for (OrderItem orderItem: orderItemList) {
                orderItem.setSubTotalMoney(orderItem.getService().getPrice() * orderItem.getQuantity());
            }
            System.out.println("该订单下的所有订单条目小计完毕!");
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("该订单下的所有订单条目小计失败！");
        }
    }

    @Override
    public List<OrderItem> getOrderItemList(Integer orderId) {
        try {
            List<OrderItem> orderItemList = this.orderItemDao.findAllByOrdersOrderId(orderId);
            System.out.println("订单"+orderId+"下的所有条目获取成功!");
            return orderItemList;
        }   catch (Exception e){
            e.printStackTrace();
            System.out.println("订单"+orderId+"下的所有条目获取失败!");
        }
        return null;
    }
}
